package frame;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

import shape.GRectangle;
import contants.GContants;

public class GDrawingPanel extends JPanel {
	private MouseDrawingHandler drawingHandler;
	private GRectangle rectangle;
	public GDrawingPanel() {
		super();
		drawingHandler = new MouseDrawingHandler();
		addMouseListener(drawingHandler);
		addMouseMotionListener(drawingHandler);
		this.setForeground(GContants.FOREGROUND_COLOR);
		this.setBackground(GContants.BACKGROUND_COLOR);
	}
	
	private void initDraw(Point originPoint) {
		rectangle = new GRectangle();
		rectangle.draw(originPoint);
	}
	
	private void animateDraw(Point currentPoint) {
		Graphics2D g2D = (Graphics2D)getGraphics();
		g2D.setXORMode(g2D.getBackground());
		g2D.draw(rectangle.getRectangle());
		rectangle.setCoordinate(currentPoint);
		g2D.draw(rectangle.getRectangle());
	}
	
	private class MouseDrawingHandler extends MouseInputAdapter {
		public void mousePressed(MouseEvent e) {
			initDraw(e.getPoint());
		}
		public void mouseDragged(MouseEvent e) {
			animateDraw(e.getPoint());
		}
	}
}
